package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.Easybuy_product;
import entity.Easybuy_user;
import utils.PageUtils;

public class EasyBuyUserDao extends BaseDao{
	
	
	Connection conn=null;
	PreparedStatement pstmt=null;
	ResultSet rs=null;
	
	public int existName(String loginName){
		int result=0;
		try {
			conn=super.getConnection();
			String sql="select count(1) from easybuy_user where loginName=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, loginName);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				result=rs.getInt(1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}
	
	public int regist(Easybuy_user user){
		int result=0;
		try {
			conn=super.getConnection();
			String sql="insert into easybuy_user(loginName,userName,password,sex,identityCode,email,mobile,type) values(?,?,?,?,?,?,?,?)";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1,user.getLoginName() );
			pstmt.setObject(2,user.getUserName() );
			pstmt.setObject(3,user.getPassword() );
			pstmt.setObject(4,user.getSex() );
			pstmt.setObject(5,user.getIdentityCode() );
			pstmt.setObject(6,user.getEmail() );
			pstmt.setObject(7,user.getMobile() );
			pstmt.setObject(8,user.getType() );
			
			result=pstmt.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
	}
	
	
	public Easybuy_user login(String name){
		Easybuy_user user=null;
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_user where loginName=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, name);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				user=new Easybuy_user();
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginName"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				user.setIdentityCode(rs.getString("identityCode"));
				user.setEmail(rs.getString("email"));
				user.setMobile(rs.getString("mobile"));
				user.setType(rs.getInt("type"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return user;
	}
	
	public PageUtils<Easybuy_user> UserList(int pageindex,int pageSize) {
		List<Easybuy_user> list=new ArrayList<Easybuy_user>();
		PageUtils<Easybuy_user> utils=new PageUtils<>();
		utils.setPagesize(pageSize);
		try {
			conn=super.getConnection();
			String sql="SELECT * from easybuy_user limit ?,? ";
			
			pstmt=conn.prepareStatement(sql);
			pstmt.setInt(1, pageindex);
			pstmt.setInt(2, pageSize);
			
			rs=pstmt.executeQuery();
			while(rs.next()) {
				Easybuy_user user=new Easybuy_user();
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginName"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				user.setIdentityCode(rs.getString("identityCode"));
				user.setEmail(rs.getString("email"));
				user.setMobile(rs.getString("mobile"));
				user.setType(rs.getInt("type"));
				list.add(user);
			}
			
			utils.setList(list);
			int total=0;
			String sqlCount="select count(1) from easybuy_user where type=0";
			
			pstmt=conn.prepareStatement(sqlCount);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				total=rs.getInt(1);
			}
			
			utils.setPagetotal(total);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return utils;
	}
	
	public Easybuy_user findById(int id){
		Easybuy_user user=null;
		try {
			conn=super.getConnection();
			String sql="select * from easybuy_user where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1, id);
			rs=pstmt.executeQuery();
			if (rs.next()) {
				user=new Easybuy_user();
				user.setId(rs.getInt("id"));
				user.setLoginName(rs.getString("loginName"));
				user.setUserName(rs.getString("userName"));
				user.setPassword(rs.getString("password"));
				user.setSex(rs.getString("sex"));
				user.setIdentityCode(rs.getString("identityCode"));
				user.setEmail(rs.getString("email"));
				user.setMobile(rs.getString("mobile"));
				user.setType(rs.getInt("type"));
			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		
		return user;
	}
	
	public int updateUser(Easybuy_user user){
		int result=0;
		try {
			conn=super.getConnection();
			String sql="update easybuy_user set loginName=?,userName=?,sex=?,identityCode=?,email=?,mobile=?,type=? where id=?";
			pstmt=conn.prepareStatement(sql);
			pstmt.setObject(1,user.getLoginName() );
			pstmt.setObject(2,user.getUserName() );
			pstmt.setObject(3,user.getSex() );
			pstmt.setObject(4,user.getIdentityCode() );
			pstmt.setObject(5,user.getEmail() );
			pstmt.setObject(6,user.getMobile() );
			pstmt.setObject(7,user.getType() );
			pstmt.setObject(8,user.getId());
			
			result=pstmt.executeUpdate();
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally {
			super.closeAll(conn, pstmt, rs);
		}
		return result;
		
	}
	


}
